package frame.project;

import java.io.File;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

public class Camera extends Activity{
	private static int TAKE_PICTURE	= 1;
	
	private Uri outputFileUri;
	String strValue;
	long date;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photo();
	}
	
	public void photo(){
		// Creates camera intent to take picture
		Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		
		// Define Calendar object to get date and time
		Calendar calendar = Calendar.getInstance();
		
		// Generates the name of the file, based on date and time, so that each of them are unique
		String imageName = Integer.toString(calendar.get(Calendar.YEAR)) + Integer.toString(calendar.get(Calendar.DAY_OF_YEAR))+Integer.toString(calendar.get(Calendar.HOUR))+ Integer.toString(calendar.get(Calendar.MINUTE))+ Integer.toString(calendar.get(Calendar.SECOND))+".jpg";
		
		// Define path to store
		File filePath = new File(Environment.getExternalStorageDirectory().toString()+"/FraMe/");
		
		// Checks if there is folder named "FraMe", if there is not, create one. If there is, automatically put the image inside the folder
		filePath.mkdir();
		
		File file = new File(filePath,imageName);
		
		// Get images' details
		date = file.lastModified();
		strValue = filePath +"/"+ imageName;
		outputFileUri = Uri.fromFile(file);
		
		i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		startActivityForResult(i, TAKE_PICTURE);
	}
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{	
	    Log.i( "MakeMachine", "resultCode: " + resultCode );
	    switch( resultCode )
	    {
	    	// User cancelled
	    	case 0:
	    		finish();
	    		break;
	    			
	    	// User decides to save the picture taken
	    	case -1:
	    		
	    		// Scan the previous picture taken (to refresh) 
	    		scanMedia(strValue);
	    		
	    		DBAdapter db = new DBAdapter(this);
	    		db.open();
	    		Cursor c = db.getPath();
	    		int id = c.getCount();
	    		db.insertPath(strValue, Long.toString(date), "Neutral", "Description..", id);
	    		db.close();
	    		
	    		// Pass the image's path to edit class, to let user have the option to edit the image or not
	    		Intent intent = new Intent(Camera.this, Edit.class);
	    		Bundle bundle = new Bundle();
	    		bundle.putString("strValue", strValue);
	    		bundle.putInt("id", id);
	    		intent.putExtras(bundle);
	    		startActivity(intent);
	    		
	    		finish();
	    		break;
	    }
	}
	
	/**
	 * Method to refresh an individual file in media store
	 * @param path - image's absolute path
	 */
	private void scanMedia(String path) {
	    File file = new File(path);
	    Uri uri = Uri.fromFile(file);
	    Intent scanFileIntent = new Intent(
	            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
	    sendBroadcast(scanFileIntent);
	}
	
}
